<template>
  <div>
    <el-card>
      <el-row>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">系统管理</el-breadcrumb-item>
          <el-breadcrumb-item> 用户管理</el-breadcrumb-item>
        </el-breadcrumb>
      </el-row>
      <!--1.多条件查询栏 -->
      <el-row>
        <el-col>
          <el-form
            :inline="true"
            :model="userSearch"
            class="demo-form-inline"
            style="margin-top: 10px"
          >
            <el-form-item label="用户姓名">
              <el-input
                v-model="userSearch.uname"
                placeholder="请输入用户姓名"
              ></el-input>
            </el-form-item>
            <el-form-item
              label="用户性别"
              style="margin-left: 30px; margin-right: 40px"
            >
              <el-radio-group v-model="userSearch.gender">
                <el-radio label="男"></el-radio>
                <el-radio label="女"></el-radio>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="用户部门">
              <el-select
                clearable
                v-model="userSearch.did"
                placeholder="请选择部门"
              >
                <el-option
                  v-for="item of departmentList"
                  :key="item.did"
                  :label="item.dname"
                  :value="item.did"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button
                icon="el-icon-search"
                type="primary"
                circle
                @click="getByCondition()"
              ></el-button>
              <el-button type="primary" @click="clearSearchForm()"
                >清空</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-divider></el-divider>
      <!--2.新增、批量删除按钮 -->
      <el-row style="margin-top: 40px; margin-bottom: 10px">
        <el-col :span="6" :offset="0">
          <el-button type="primary" @click="addClick()">添加</el-button>
          <el-button type="danger" @click="batchDeleteClick()"
            >批量删除</el-button
          >
        </el-col>
      </el-row>
      <!--3.用户表单 -->
      <el-row>
        <el-col>
          <el-table
            :data="userList"
            style="width: 100%"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55"> </el-table-column>
            <el-table-column label="编号" width="70">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.uid }}</span>
              </template>
            </el-table-column>

            <el-table-column label="姓名" width="100">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.uname }}</span>
              </template>
            </el-table-column>
            <el-table-column label="性别" width="100" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.gender }}</span>
              </template>
            </el-table-column>
            <el-table-column label="联系电话" width="150" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.phone }}</span>
              </template>
            </el-table-column>
            <el-table-column label="部门" width="150" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{
                  scope.row.department.dname
                }}</span>
              </template>
            </el-table-column>
            <el-table-column label="状态" width="100" align="center">
              <template slot-scope="scope">
                <div
                  style="margin-left: 10px"
                  :class="statusClass(scope.row.status)"
                >
                  {{ getStatus(scope.row.status) }}
                </div>
              </template>
            </el-table-column>

            <el-table-column label="创建时间" width="200" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{
                  scope.row.createtime
                }}</span>
              </template>
            </el-table-column>

            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  round
                  plain
                  size="mini"
                  type="success"
                  @click="showDepartmentDialog(scope.$index, scope.row)"
                  >分配部门</el-button
                >
                <el-button
                  round
                  plain
                  size="mini"
                  type="primary"
                  @click="handleEdit(scope.$index, scope.row)"
                  >修改</el-button
                >

                <el-button
                  plain
                  round
                  size="mini"
                  type="info"
                  v-if="scope.row.status == 0"
                  @click="setStatus(scope.$index, scope.row)"
                  >启用</el-button
                >
                <el-button
                  plain
                  round
                  size="mini"
                  type="warning"
                  v-if="scope.row.status == 1"
                  @click="setStatus(scope.$index, scope.row)"
                  >禁用</el-button
                >
                <el-button
                  plain
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <!--4.分页 -->
      <el-row style="margin-top: 40px">
        <el-col>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[2, 3, 4, 5]"
            :page-size="size"
            layout="prev, pager, next, jumper,total, sizes"
            :total="total"
          >
          </el-pagination>
        </el-col>
      </el-row>

      <!-- 5.分配部门对话框 -->
      <el-dialog
        title="分配部门"
        :visible.sync="dialogVisible"
        @close="closeAllocateDialog"
      >
        <el-tree
          :data="departmentList"
          :props="defaultProps"
          show-checkbox
          :expand-on-click-node="false"
          :node-key="nodeKey"
          ref="tree"
          @check-change="handleCheckChange"
          multiple="false"
          :default-checked-keys="existDepartment"
        >
        </el-tree>
        <span slot="footer" class="dialog-footer">
          <el-button @click="closeAllocateDialog">取 消</el-button>
          <el-button type="primary" @click="allocateDepartment"
            >确 定</el-button
          >
        </span>
      </el-dialog>

      <!-- 6.添加修改对话框-->
      <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogFormVisible"
        @close="closeDialog"
        center
      >
        <el-row>
          <el-col :span="16" :offset="4">
            <el-form
              :model="userModify"
              label-width="100px"
              ref="employeeModify"
            >
              <el-form-item label="用户姓名" prop="uname">
                <el-input v-model="userModify.uname"></el-input>
              </el-form-item>

              <el-form-item label="用户性别" prop="gender">
                <el-radio-group v-model="userModify.gender">
                  <el-radio label="男"></el-radio>
                  <el-radio label="女"></el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="联系方式" prop="phone">
                <el-input v-model="userModify.phone"></el-input>
              </el-form-item>

              <el-form-item label="所属部门">
                <el-select
                  v-model="userModify.did"
                  placeholder="请选择部门"
                  style="width: 450px"
                  :disabled="isDisabled"
                >
                  <el-option
                    v-for="item of departmentList"
                    :key="item.did"
                    :label="item.dname"
                    :value="item.did"
                  ></el-option>
                </el-select>
              </el-form-item>

              <el-form-item>
                <el-row style="margin-top: 50px">
                  <el-col :span="10" :offset="7">
                    <el-button
                      type="primary"
                      @click="addUser()"
                      v-if="dialogTitle == '添加用户'"
                      >添加</el-button
                    >
                    <el-button
                      type="primary"
                      @click="modifyUser()"
                      v-if="dialogTitle == '修改用户'"
                      >修改</el-button
                    >

                    <el-button
                      type="info"
                      @click="closeDialog"
                      style="margin-left: 40px"
                      >取消</el-button
                    >
                  </el-col>
                </el-row>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //1.查询
      userSearch: {},
      departmentList: [],
      //2.表格
      multipleSelection: [],
      userList: [],
      //3.分页
      page: 1,
      size: 3,
      total: 0,
      isPageChangeToOne: true,
      //4.分配部门
      dialogVisible: false,
      defaultProps: {
        // children: "children",
        label: "dname",
      },
      existDepartment: [],
      nodeKey: "did",
      allocateParam: {},

      //5.添加修改
      formLabelWidth: "120px",
      dialogTitle: "添加用户",
      dialogFormVisible: false,
      isDisabled: false,
      userModify: {},
    };
  },
  methods: {
    //------------------------表单
    //获取部门列表
    getDepartmentList() {
      this.$axios.get("/department/getAll").then((respData) => {
        console.log(respData);
        this.departmentList = respData.data.data;
      });
    },
    //多条件查询
    getByCondition() {
      this.$axios
        .post(
          "/user/getByCondition/" + this.page + "/" + this.size,
          this.userSearch
        )
        .then((respData) => {
          console.log(respData);
          this.userList = respData.data.data.beanList;
          this.page = respData.data.data.pageIndex;
          this.total = respData.data.data.totalRecord;
        });
    },
    //清空多条件查询表单
    clearSearchForm() {
      this.userSearch = {};
      this.getByCondition();
    },
    // -----------------------表格
    //添加按钮
    addClick() {
      this.dialogFormVisible = true;
    },
    //批量删除按钮
    batchDeleteClick() {
      if (this.multipleSelection.length == 0) {
        this.$message.error("请至少选择一条数据！");
      } else {
        this.$confirm("确定删除这些数据吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            let deleteParam = "?";
            for (var index in this.multipleSelection) {
              deleteParam += "uids=" + this.multipleSelection[index].uid + "&";
            }

            this.$axios
              .delete("/user/batchedDelete" + deleteParam)
              .then((respData) => {
                if (respData.data.data) {
                  this.$message.success("批量删除成功！");
                  this.getByCondition();
                }
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      }
    },
    //表格多选框事件
    handleSelectionChange(rows) {
      this.multipleSelection = rows;
      console.log(this.multipleSelection);
    },
    //表格编辑事件
    handleEdit(index, row) {
      this.dialogTitle = "修改用户";
      this.userModify = row;
      this.userModify.did = row.department.did;
      this.isDisabled = true;
      this.dialogFormVisible = true;
    },
    //表格删除事件
    handleDelete(index, row) {
      this.$confirm("确定删除" + row.uname + "这条信息吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios.delete("/user/deleteById/" + row.uid).then((respData) => {
            console.log(respData);
            if (respData.data.data) {
              this.$message.success("删除成功！");
              this.getByCondition();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //表格分配部门点击事件
    showDepartmentDialog(index, row) {
      this.allocateParam.uid = row.uid;
      this.existDepartment.push(row.department.did);

      console.log("existDepartment",this.existDepartment);
      // this.$refs.tree.setCheckedKeys(this.existDepartment);

      this.$nextTick(() => {
        this.$refs.tree.setCheckedKeys(this.existDepartment);
      });

      this.dialogVisible = true;
    },

    //表格设置状态
    setStatus(index, row) {
      let status = 0;
      if (row.status == 0) {
        status = 1;
      }
      this.$axios
        .put("/user/modifyStatus/" + row.uid + "/" + status)
        .then((respData) => {
          console.log(respData);
          if (respData.data.data) {
            if (status == 1) {
              this.$message.success("启用成功！");
            } else {
              this.$message.success("禁用成功！");
            }
            this.getByCondition();
          }
        });
    },

    //数据渲染
    getStatus(status) {
      if (status == 0) {
        return "已禁用";
      }

      if (status == 1) {
        return "已启用";
      }
    },
    statusClass(status) {
      switch (status) {
        case 0:
          return "tag1";
        case 1:
          return "tag2";
      }
    },

    //--------------------------分页
    //分页尺寸变化
    handleSizeChange(size) {
      this.size = size;
      this.getByCondition();
    },
    //分页页码变化
    handleCurrentChange(page) {
      this.isPageChangeToOne = false;
      this.page = page;
      this.getByCondition();
      this.isPageChangeToOne = true;
    },

    //--------------------------分配部门对话框
    //树节点点击事件
    handleCheckChange(data, checked, indeterminate) {
      console.log(data, checked, indeterminate);
      if (checked) {
        this.$refs.tree.setCheckedNodes([data]);
      }
    },
    //分配
    allocateDepartment() {
      this.allocateParam.did = this.$refs.tree.getCheckedKeys()[0];
      console.log("allocateParam",this.allocateParam);
      this.$axios
        .put("/user/allocateDepartment", this.allocateParam)
        .then((respData) => {
          if (respData.data.data) {
            this.$message.success("分配成功！");
            this.dialogVisible = false;
            this.allocateParam = {};
            this.getByCondition();
          }
        });
    },

    //关闭分配部门对话框
    closeAllocateDialog() {
      this.existDepartment = [];
      this.$refs.tree.setCheckedKeys(this.existDepartment);
      this.dialogVisible = false;
    },

    //关闭添加修改对话框
    closeDialog() {
      this.userModify = {};
      this.disabled = false;
      this.dialogFormVisible = false;
    },
    //添加用户
    addUser() {
      this.$axios.post("/user/addUser", this.userModify).then((respData) => {
        if (respData.data.data) {
          this.$message.success("添加成功！");
          this.userModify = {};
          this.dialogFormVisible = false;
          this.getByCondition();
        }
      });
    },
    //修改用户
    modifyUser() {
      this.$axios.put("/user/modifyUser", this.userModify).then((respData) => {
        if (respData.data.data) {
          this.$message.success("修改成功！");
          this.userModify = {};
          this.dialogFormVisible = false;
          (this.disabled = false), this.getByCondition();
        }
      });
    },
  },
  mounted() {
    this.getDepartmentList();
    this.getByCondition();
  },
};
</script>

<style scoped>
.tag1 {
  text-align: center;
  border-radius: 12px;
  background: #e8f8f3;
  border: 1px solid #fa556e;
  color: #fa5563;
}

.tag2 {
  text-align: center;
  border-radius: 12px;
  background: #e8f8f3;
  border: 1px solid #21bd88;
  color: #21bd88;
}
</style>
